<template>
	<view class="page-warp">
		<view class="search">
			<view class="search-input">
				<text class="yzb yzb-search"></text>
				<input class="text-normal" type="text" :value="searchValue" :placeholder="placeholder"
					@input="searchInput" />
			</view>
			<view class="btn-cancel" @click="cancelSearch">取消</view>
		</view>
		<view class="center-warp">
			<!-- 左边 -->
			<scroll-view class="left-warp" :scroll-y="true">
				<view class="tab" :class="{active:i==tabIndex}" v-for="(tab,i) in tabs" :key="i" @click="tabChange(i)">
					{{tab.title}}</view>
			</scroll-view>
			<view class="right-warp">
				<!--右边 :fixed="false", 高度跟随父元素 (不在组件上定义class,避免部分小程序平台编译丢失, 如支付宝,钉钉小程序) -->
				<mescroll-uni :fixed="false" ref="mescrollRef" :up="upOption" @init="mescrollInit" @down="downCallback"
					@up="upCallback">
					<train-list :list="list" @click="toDetail"></train-list>
				</mescroll-uni>
			</view>
		</view>
		<!-- <page-tabpars></page-tabpars> -->
	</view>
</template>


<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				list: [],
				tabs: [{
					children: null,
					key: "",
					leaf: true,
					parentId: "",
					title: "全部",
					value: null
				}],
				tabIndex: 0, // tab下标
				
				query:{
					pageNo: 1,
					pageSize: 10,
					trainType:'',
					keyword:''
				},
				searchValue: '',
				placeholder: '请输入关键词搜索相关内容',
				
				upOption: {
					onScroll: true,
					auto: false, // 不自动加载
					noMoreSize: 5, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						tip: '空空如也', // 提示
						icon: 'http://cdn.uviewui.com/uview/empty/data.png'
					}
				},
			}
		},

		onLoad() {
		},


		methods: {

			async getTrainList(pageNo,pageSize) {
				this.query.pageNo=pageNo;
				this.query.pageSize=pageSize;
				this.$apis.getTrainList(this.query).then(res => {
					console.log('getTrainList', res);
					if (pageNo == 1) {
						this.list = [];
					}
					if (res) {
						let data=res.records;
						this.list = this.list.concat(data);
						this.mescroll.endSuccess(res.records.length);
					}
				});
			},

			toDetail(item) {
				console.log("toDetail===", item);
				this.$mRouter.push({
					route: this.$mRoutesConfig.trainDetail,
					query: {
						id: item.id
					}
				});
			},

			upCallback(page) {
				// tabs异步获取
				if (this.tabs.length == 1) {
					this.$apis.getTrainTypeList().then(res => {
						// this.tabs = res
						this.tabs=this.tabs.concat(res);
						console.log("");
						this.mescroll.resetUpScroll() // 重新触发upCallback
					}).catch(() => {
						this.mescroll.endErr()
					})
					return // 此处return,先获取tabs
				}
				this.getTrainList(page.num,page.size);
			},
			// 切换菜单
			tabChange(i) {
				if (this.tabIndex != i) {
					this.tabIndex = i
					this.query.trainType=this.tabs[i].key;
					this.list = []; // 先置空列表,显示加载进度条
					this.mescroll.resetUpScroll(); // 重置列表数据
				}
			},
			
			searchInput(e) {
				this.searchValue = e.detail.value;
				console.log('searchInput', this.searchValue);
				if (this.searchValue) {
					this.query.keyword = this.searchValue;
					this.query.pageNo = 1;
					this.getTrainList(1,this.query.pageSize);
				} else {
					this.cancelSearch();
				}
			},
			// 取消搜索
			cancelSearch() {
				this.searchValue = '';
				this.query.keyword = '';
				this.query.pageNo = 1;
				this.getTrainList(1,this.query.pageSize);
			},
		}
	}
</script>


<style lang="scss">
	/*根元素需要有固定的高度*/
	page {
		height: 100%;
		// 支付宝小程序,钉钉小程序需添加绝对定位,否则height:100%失效: https://opendocs.alipay.com/mini/framework/acss#%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
		/* #ifdef MP-ALIPAY || MP-DINGTALK*/
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		/* #endif */

		/*需给父元素设置height:100%*/
		.page-warp {
			height: 100%;
			display: flex;
			flex-direction: column;

			/* 顶部区域 */
			.top-warp {
				font-size: 28rpx;
				padding: 20rpx;
				text-align: center;
				background-color: #CFE0DA;
			}

			.search {
				width: 100%;
				display: flex;
				height: 90upx;
				z-index: 999;
				flex-direction: row;
				background-color: $main-color;
				color: #ffffff;
				align-items: center;
				justify-content: center;

				.yzb-search {
					color: $font-color-999;
					font-size: $uni-font-size-base;
					margin: 0 15upx;
					margin-top: 8upx;
				}

				.search-input {
					width: 77%;
					height: 65upx;
					border-radius: 50upx;
					display: flex;
					flex-direction: row;
					background-color: #ffffff;
					align-items: center;
				}

				.search-input image {
					width: 35upx;
					height: 35upx;
					margin-right: 10upx;
				}

				.search-input input {
					width: 65%;
					font-size: 28upx;
					color: $font-color-333;
				}

				.btn-search {
					margin-left: 20upx;
				}

				.btn-cancel {
					margin-left: 20upx;
				}
			}

			/* 中间 */
			.center-warp {
				flex: 1;
				min-width: 0;
				min-height: 0;
				/* 需给flex:1的元素加上最小高,否则内容超过会溢出容器 (如:小程序Android真机) */
				display: flex;

				// 左边
				.left-warp {
					width: 180rpx;
					height: 100%;
					background-color: #eee;

					.tab {
						font-size: 28rpx;
						padding: 28rpx;

						&.active {
							background-color: #fff;
						}
					}
				}

				// 右边
				.right-warp {
					flex: 1;
					min-width: 0;
				}
			}

			/* 底部区域 */
			.bottom-warp {
				padding: 20rpx;
				text-align: center;
				background-color: #FF6990;
			}
		}
	}
</style>